package com.xhd.xml;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.file.XZipFile;
import com.xhd.server.ServerConfig;

public class RptXMLBuilder
{
  private static final String jm621 = "~rpt_result";
  private static int jm622 = 0;
  private String jm623 = null;
  private boolean jm624 = true;
  public static final String header = "<DATAPACKET Version=\"2.0\">\n";
  public static final String tailer = "</DATAPACKET>";
  public static final String metaDataBegin = "<METADATA>\n<FIELDS>\n";
  public static final String metaDataEnd = "</FIELDS>\n<PARAMS/>\n</METADATA>\n";
  public static final String rowDataBegin = "<ROWDATA>\n";
  public static final String rowDataEnd = "</ROWDATA>\n";
  private Vector jm625;
  private Vector jm146;
  private static final Log jm9;
  static Class jm10;

  static
  {
    Class tmp7_4 = jm10;
    if (tmp7_4 == null)
    {
 
      try
      {
      RptXMLBuilder.jm10 = Class.forName("com.sinocc.base.xml.RptXMLBuilder");
      }
      catch (ClassNotFoundException localClassNotFoundException)
      {
        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
      }
    }
    jm9 = LogFactory.getLog(tmp7_4);
  }

  public RptXMLBuilder(Vector paramVector1, Vector paramVector2)
  {
    this.jm625 = paramVector1;
    this.jm146 = paramVector2;
    this.jm623 = ServerConfig.RptXMLEncode;
    this.jm624 = ServerConfig.IsZipRptXML;
  }

  public RptXMLBuilder(Vector paramVector1, Vector paramVector2, String paramString, boolean paramBoolean)
  {
    this.jm625 = paramVector1;
    this.jm146 = paramVector2;
    this.jm623 = paramString;
    this.jm624 = paramBoolean;
  }

  public String genXMLFile()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    Object localObject1 = null;
    int i = 0;
    int j = 0;
    if (this.jm625 != null)
      i = this.jm625.size();
    if (this.jm146 != null)
      j = this.jm146.size();
    try
    {
      boolean[] arrayOfBoolean = new boolean[i];
      localStringBuffer.append("<?xml version=\"1.0\" encoding=\"");
      localStringBuffer.append(this.jm623);
      localStringBuffer.append("\"?>\n");
      localStringBuffer.append("<DATAPACKET Version=\"2.0\">\n");
      localStringBuffer.append("<METADATA>\n<FIELDS>\n");
      String str1 = "";
      Object localObject4;
      int l;
      for (int k = 0; k < i; ++k)
      {
    	 Vector  localObject3 = (Vector)this.jm625.elementAt(k);
        localObject4 = new String[4];
        for (l = 0; l < ((String[])localObject4).length; ++l)
        ((String[])localObject4)[l] = ((String)((Vector)localObject3).elementAt(l));
        if (((String[])localObject4)[0] == null || ((((String[])localObject4)[0]).equals("")))
          ((String[])localObject4)[0] = "column" + String.valueOf(k + 1);
        arrayOfBoolean[k] = false;
        if (((String[])localObject4)[1].equals("r8"))
        {
        	((String[])localObject4)[2] = "16";
        }
        else if (((String[])localObject4)[1].equals("i4"))
        {
        	((String[])localObject4)[2] = "10";
        }
        else if (((String[])localObject4)[1].equals("datetime"))
        {
        	((String[])localObject4)[2] = "19";
          arrayOfBoolean[k] = true;
        }
        str1 = str1 + "<FIELD fieldname=\"" + ((String[])localObject4)[0] + "\" attrname=\"x" + String.valueOf(k + 1) + "\" fieldtype=\"" + ((String[])localObject4)[1] + "\" width=\"" + ((String[])localObject4)[2] + "\" scale=\"" + ((String[])localObject4)[3] + "\"/>\n";
      }
      localStringBuffer.append(str1);
      localStringBuffer.append("</FIELDS>\n<PARAMS/>\n</METADATA>\n");
      if (this.jm146 != null)
      {
    	 Object localObject2 = "";
    	 Object  localObject3 = null;
    	localObject4 = null;
        localStringBuffer.append("<ROWDATA>\n");
        for (l = 0; l < j; ++l)
        {
          Vector localVector = (Vector)this.jm146.elementAt(0);
          localObject3 = "";
          for (int i1 = 0; i1 < i; ++i1)
          {
            localObject4 = (String)localVector.elementAt(i1);
            if ((arrayOfBoolean[i1] != 0) && (localObject4 != null))
              localObject4 = ((String)localObject4).replace(' ', 'T');
            localObject3 = localObject3 + " x" + String.valueOf(i1 + 1) + "=\"" + DataFilter.filterStringToXML((String)localObject4) + "\"";
          }
          localObject2 = "<ROW" + ((String)localObject3) + "/>\n";
          localStringBuffer.append((String)localObject2);
          this.jm146.remove(0);
        }
        localStringBuffer.append("</ROWDATA>\n");
      }
      localStringBuffer.append("</DATAPACKET>");
      Object localObject2 = new String(localStringBuffer).getBytes(this.jm623);
      Object localObject3 = "~rpt_result" + jm620();
      if (!(this.jm624))
      {
        localObject3 = ServerConfig.getTempPath() + ((String)localObject3) + ".xml";
        localObject1 = new FileOutputStream((String)localObject3, false);
      }
      else
      {
        localObject4 = new XZipFile();
        String str2 = ServerConfig.getTempPath() + ((String)localObject3) + ".zip";
        localObject1 = ((XZipFile)localObject4).getZipOutput(str2, localObject3 + ".xml");
        localObject3 = str2;
      }
      ((OutputStream)localObject1).write(localObject2);
      ((OutputStream)localObject1).flush();
      ((OutputStream)localObject1).close();
      localObject1 = null;
      this.jm625.removeAllElements();
      this.jm146.removeAllElements();
      localStringBuffer = null;
      localObject2 = (byte[])null;
      return (String) localObject3;
    }
    catch (Throwable localThrowable)
    {
      jm9.error(localThrowable);
      if (localObject1 != null)
        try
        {
          ((OutputStream)localObject1).close();
        }
        catch (Exception localException)
        {
        }
    }
    return ((String)(String)(String)(String)null);
  }

  private synchronized String jm620()
  {
    if (jm622 > 99)
      jm622 = 0;
    jm622 += 1;
    return String.valueOf(jm622);
  }
}